package com.youfang.sixthmedia.ui.center;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.gzit.common.async.AsynCallback;
import com.gzit.utils.AndroidUtils;
import com.gzit.utils.StringUtils;
import com.youfang.sixthmedia.R;
import com.youfang.sixthmedia.common.YoufangContext;
import com.youfang.sixthmedia.message.LoginRequest;
import com.youfang.sixthmedia.message.LoginResponse;
import com.youfang.sixthmedia.message.center.RegMessageDelegate;
import com.youfang.sixthmedia.message.model.User;
import com.youfang.sixthmedia.net.Net;
import com.youfang.sixthmedia.net.Response;

public class CRegActivity extends Activity {

	TextView username;
	TextView email;
	TextView password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.c_reg_activity);

		username = (TextView) this.findViewById(R.id.username);
		email = (TextView) this.findViewById(R.id.email);
		password = (TextView) this.findViewById(R.id.password);

		this.findViewById(R.id.regbtn).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						remoteReg();

					}
				});

	}

	private void remoteReg() {

		if (StringUtils.isBlank(username.getText().toString())) {
			AndroidUtils.toastShort("用户名不能为空");
			return;

		}

		if (StringUtils.isBlank(email.getText().toString())) {
			AndroidUtils.toastShort("邮箱不能为空");
			return;

		}

		if (StringUtils.isBlank(password.getText().toString())) {
			AndroidUtils.toastShort("密码不能为空");
			return;

		}

		final RegMessageDelegate delegate = new RegMessageDelegate();
		delegate.req.email = email.getText().toString();
		delegate.req.username = username.getText().toString();
		delegate.req.password = password.getText().toString();

		Net.message(this, delegate, new AsynCallback<Response>() {

			@Override
			public void callback(Response r) {
				if (r.isFail()) {
					AndroidUtils.toastShort("注册失败，原因：" + r.getError());
					return;

				}

				AndroidUtils.toastShort("注册成功");

				// process login logic...
				remoteLogin(delegate.req.username, delegate.req.password);

			}
		});

	}

	private void remoteLogin(String userName, final String password) {
		LoginRequest req = new LoginRequest();
		req.setUsername(userName);
		req.setPassword(password);

		Net.doHttpRemote(this, req, new LoginResponse(),
				new AsynCallback<Response>() {

					@Override
					public void callback(Response r) {
						if (!r.isOk()) {
							AndroidUtils.toastShort("登录失败");
							return;
						}

						// 登录成功...
						LoginResponse resp = (LoginResponse) r;
						User user = resp.getUser();

						// save user infomation
						user.password = password;
						YoufangContext.saveUserAndStartMessageService(user);

						// 返回
						setResult(Activity.RESULT_OK);
						finish();

					}
				});

	}

}
